<?php

header('content-type:text/html;charset=utf-8');

class CurlController extends Curl
{

	const WEB_INDEX = 'http://203.93.209.205:48339/';

	private $data = []; // 用户信息

	public function __construct()
	{
		parent::__construct();

		$this->login();
	}

	private function setLoginParam()
	{
		$this->data['tbYHM'] = '201393411209';

		$this->data['tbPSW'] = '651212';

		$this->data['imgDL.x'] = '31';

		$this->data['imgDL.y'] = '13';

		$this->data['ddlSF'] = iconv('utf-8', 'GBK', '学生'); // 学生 或者 老师

		$view = $this->getView();

		foreach ($view as $v) {
			$this->data[$v[1]] = $v[2];
		}
	}

	/**
	* 模拟登陆
	*/
	private function login()
	{
		$this->setLoginParam();
		
		$this->post(SELF::WEB_INDEX, $this->data);

		$res_header = $this->response_headers;
		
		foreach ($res_header as $v) {
			if(strpos($v, 'Set-Cookie') !== false){
				preg_match('/Set-Cookie:\s(.*?);/i', $v, $res);
				$cookie = explode('=', $res[1]);
				$this->setCookie($cookie[0], $cookie[1]);
				break;
			}
		}
	}

	/**
	* 获取登陆页面的View值
	*/
	private function getView()
	{
		$this->get(SELF::WEB_INDEX);
		
		$result = $this->response;

		$pattern = '/<input type="hidden" name="(.*?)" value="(.*?)" \/>/i';

		preg_match_all($pattern, $result, $view, PREG_SET_ORDER);

		return $view;
	}

	/**
	* 学生信息查询
	*/
	public function getStuInfo() {
		$url = SELF::WEB_INDEX . "xstop.aspx?xh={$this->data['tbYHM']}";

		$this->get($url);

		echo iconv('GBK', 'utf-8', $this->response);
	}
}
